package com.qf.cx.simplenms.webservice.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.qf.cx.simplenms.persist.entity.AntennaEntity;
import com.qf.cx.simplenms.persist.entity.ReaderEntity;

public class Reader {
	private String name;
	private String ip;
	private int port;
	private String deviceSn;
	private int linkType;
	private Set<Antenna> antennas = Collections.emptySet();

	public String getName() {
		return name;
	}

	public String getIp() {
		return ip;
	}

	public int getPort() {
		return port;
	}

	public String getDeviceSn() {
		return deviceSn;
	}

	public int getLinkType() {
		return linkType;
	}

	public Set<Antenna> getAntennas() {
		return antennas;
	}

	public void setAntennas(Set<Antenna> antennas) {
		this.antennas = antennas;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setDeviceSn(String deviceSn) {
		this.deviceSn = deviceSn;
	}

	public void setLinkType(int type) {
		this.linkType = type;
	}

	public static Reader from(ReaderEntity readerEntity) {
		Reader r = new Reader();
		r.ip = readerEntity.getIp();
		r.port = readerEntity.getPort();
		r.name = readerEntity.getName();
		r.deviceSn = readerEntity.getDeviceSn();
		r.linkType = readerEntity.getLinkType();
		r.antennas = new HashSet<>();
		readerEntity.getAntennaEntities().forEach(t -> {
			Antenna antenna = new Antenna();
			antenna.setId(t.getId());
			antenna.setAntenna(t.getAntenna());
			antenna.setProtocol(t.getProtocol());
			r.antennas.add(antenna);
		});
		return r;
	}

	public ReaderEntity toReaderEntity() {
		ReaderEntity readerEntity = new ReaderEntity();
		readerEntity.setIp(ip);
		readerEntity.setName(name);
		readerEntity.setPort(port);
		readerEntity.setDeviceSn(deviceSn);
		readerEntity.setLinkType(linkType);
		List<AntennaEntity> antennaEntities = new ArrayList<>();

		antennas.forEach(t -> {
			AntennaEntity antennaEntity = new AntennaEntity();
			antennaEntity.setId(t.getId());
			antennaEntity.setAntenna(t.getAntenna());
			antennaEntity.setProtocol(t.getProtocol());
			antennaEntity.setReaderEntity(readerEntity);
			antennaEntities.add(antennaEntity);
		});
		readerEntity.setAntennaEntities(antennaEntities);
		return readerEntity;
	}

	@Override
	public String toString() {
		return "Reader [name=" + name + ", ip=" + ip + ", port=" + port + ", deviceSn=" + deviceSn + ", linkType="
				+ linkType + ", antennas=" + antennas + "]";
	}
	
}
